
***************************************************************************************************************
/*
THIS DO-FILE:
Gets 1998 (wave 4) helper information from raw HRS data
*/
***************************************************************************************************************

clear
set more off
set maxvar 10000

***************************************************************************************************************
use "$rawdata\h98fin\h98sta\H98E_R.DTA", clear
*ADLS: must use this dataset to identify OPN=036 (spouse), 100 (employee of facility) -- those codes not in HP file below
rename F2502 ADLhelper1
rename F2516 ADLhelper2
rename F2525 ADLhelper3
rename F2529 ADLhelper4
rename F2533 ADLhelper5
rename F2537 ADLhelper6
rename F2541 ADLhelper7

reshape long ADLhelper, i(HHID PN) j(ADLhelpernum)
drop if ADLhelper==""
rename ADLhelper OPN

duplicates tag HHID PN OPN, gen(duptag)
bys HHID PN OPN: egen minnum=min(ADLhelpernum)
keep if duptag==0 | (duptag>0 & ADLhelpernum==minnum)

tempfile ADLhelperfile
save `ADLhelperfile', replace
***************************************************************************************************************

***************************************************************************************************************
use "$rawdata\h98fin\h98sta\H98E_R.DTA", clear
*IADLS: must use this dataset to identify OPN=036 (spouse), 100 (employee of facility) -- those codes not in HP file below
rename F2582 IADLhelper1
rename F2591 IADLhelper2
rename F2596 IADLhelper3
rename F2602 IADLhelper4
rename F2608 IADLhelper5
rename F2614 IADLhelper6

reshape long IADLhelper, i(HHID PN) j(IADLhelpernum)
drop if IADLhelper==""
rename IADLhelper OPN

duplicates tag HHID PN OPN, gen(duptag)
bys HHID PN OPN: egen minnum=min(IADLhelpernum)
keep if duptag==0 | (duptag>0 & IADLhelpernum==minnum)

tempfile IADLhelperfile
save `IADLhelperfile', replace
***************************************************************************************************************

***************************************************************************************************************
use "$rawdata\h98fin\h98sta\H98E_R.DTA", clear
*money: must use this dataset to identify OPN=036 (spouse), 100 (employee of facility) -- those codes not in HP file below
rename F2621 MONEYhelper1
rename F2625 MONEYhelper2

reshape long MONEYhelper, i(HHID PN) j(MONEYhelpernum)
drop if MONEYhelper==""
rename MONEYhelper OPN

duplicates tag HHID PN OPN, gen(duptag)
bys HHID PN OPN: egen minnum=min(MONEYhelpernum)
keep if duptag==0 | (duptag>0 & MONEYhelpernum==minnum)

tempfile MONEYhelperfile
save `MONEYhelperfile', replace
***************************************************************************************************************

use "$rawdata\h98fin\h98sta\H98E_HP.DTA", clear
merge 1:1 HHID PN OPN using `ADLhelperfile', gen(mergeADL)
merge 1:1 HHID PN OPN using `IADLhelperfile', gen(mergeIADL)
merge 1:1 HHID PN OPN using `MONEYhelperfile', gen(mergemoney)

egen LOPN=concat(FSUBHH OPN)

destring HHID, replace
destring PN, replace
gen double hhidpn=1000*HHID+PN
destring OPN, replace
destring LOPN, replace
gen double kidid=10000*HHID+LOPN

*helper type
gen helper_spouse=(OPN==36)  //no other info asked since not in H89E_HP.dta
gen helper_nonfam=(OPN==100) //no other info asked since not in H89E_HP.dta
replace helper_nonfam=inlist(F2639A,61,62,63,71,72,73) if helper_nonfam==0
gen helper_child=inlist(F2639A,11,12,13,14)
gen helper_othfam=inlist(F2639A,21,22,31,32,41,51,52)
foreach var in helper_spouse helper_nonfam helper_child helper_othfam {
	replace `var'=. if inlist(helper_spouse,0,.) & inlist(helper_nonfam,0,.) & inlist(helper_child,0,.) & inlist(helper_othfam,0,.)
}
*10 out of 5102 are missing -- they won't be counted as helpers

*helper paid?
gen helper_paid=(OPN==100 | F2649==1) if helper_nonfam==1

*helper frequency - converted to days per month
gen 	helper_dpm=F2642 if F2642<=31  //reported days per month
replace helper_dpm=F2643*4 if F2643<=7 //reported days per week
replace helper_dpm=30 if F2644==1	   //reported every day
replace helper_dpm=F2658 if F2658<=31 & helper_dpm==.
replace helper_dpm=F2659*4 if F2659<=7 & helper_dpm==.
replace helper_dpm=30 if F2660==1 & helper_dpm==.
replace helper_dpm=0 if helper_dpm==. & (F2642==96 | F2658==96)
*impute the "DK" responses with median for that type of helper
forval x=10/73 {
	qui sum helper_dpm if F2639A==`x', det
	qui replace helper_dpm=r(p50) if F2639A==`x' & (inlist(F2642,98,99) | inlist(F2643,98,99) | F2644==8 | inlist(F2658,98,99)) & helper_dpm==.
}

*helper hours per day
gen 	helper_hpd=F2646 if F2646<=24
replace helper_hpd=F2662 if F2662<=24 & helper_hpd==.
*impute the "DK" responses by family/other and days per month categories
foreach var in helper_spouse helper_child helper_othfam helper_nonfam {
	sum helper_hpd if `var'==1 & helper_dpm>=1 & helper_dpm<=7, det
	replace helper_hpd=r(p50) if `var'==1 & helper_dpm>=1 & helper_dpm<=7 & (inlist(F2646,98,99) | inlist(F2662,98,99)) & helper_hpd==.
	sum helper_hpd if `var'==1 & helper_dpm>=8 & helper_dpm<=27, det
	replace helper_hpd=r(p50) if `var'==1 & helper_dpm>=8 & helper_dpm<=27 & (inlist(F2646,98,99) | inlist(F2662,98,99)) & helper_hpd==.
	sum helper_hpd if `var'==1 & helper_dpm>=28 & helper_dpm<=31, det
	replace helper_hpd=r(p50) if `var'==1 & helper_dpm>=28 & helper_dpm<=31 & (inlist(F2646,98,99) | inlist(F2662,98,99)) & helper_hpd==.
}

*hours per month
gen		helper_hpm=helper_hpd*helper_dpm
*those who report 0 or . dpm or hpd won't be counted as helpers unless AL/NH helper

*type of care
egen helper_fam=rowtotal(helper_spouse helper_child helper_othfam), missing
bys hhidpn: egen rec_inf=max(helper_fam)
bys hhidpn: egen rec_form=max(helper_nonfam)
bys hhidpn: egen rec_paid=max(helper_paid)

*hours - total over all of person's helpers
gen hrs_inf_temp=helper_hpm if helper_fam==1
bys hhidpn: egen hrs_inf=sum(hrs_inf_temp)
gen hrs_form_temp=helper_hpm if helper_nonfam==1
bys hhidpn: egen hrs_form=sum(hrs_form_temp)
egen hrs_total=rowtotal(hrs_inf hrs_form)

***************************************************************************************************************
*ADL AND IADL (including money) HELPERS (up to 7)
preserve

keep hhidpn OPN LOPN kidid helper_hpm helper_spouse helper_child helper_othfam helper_nonfam helper_paid ///
	 rec_inf rec_form hrs_inf hrs_form hrs_total
foreach var in helper_hpm helper_spouse helper_child helper_othfam helper_nonfam helper_paid rec_inf rec_form hrs_inf hrs_form hrs_total {
	rename `var' `var'4
}
duplicates drop
save "allADLIADLhelpers_4.dta", replace
restore
***************************************************************************************************************
